home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 5626 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: zib-berlin.de!uniol!news
  2. From: Ingo.Wilken@Informatik.Uni-Oldenburg.DE (Ingo Wilken)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: My code crashes my computer
  5. Date: 17 Mar 96 21:42:27 GMT
  6. Organization: C. v. Ossietzky Universitaet Oldenburg - Fachbereich Informatik
  7. Message-ID: <4ii1jk$j8h@news.Informatik.Uni-Oldenburg.DE>
  8. References: <4igm25$poj@goofy.BrandonU.CA>
  9. NNTP-Posting-Host: rubin.informatik.uni-oldenburg.de
  10. X-Newsreader: NN version 6.5.0 #2 (NOV)
  11.  
  12. menon@beijing.brandonu.ca writes:
  13. >void read_file(char *infile, char *infile2){
  14. >   int i=0, x, y, garb;
  15. >   FILE *fp;
  16. >   char *s="File Error";
  17. >   char *comment="", buf;
  18.  
  19. >   fgets(type,3,fp);
  20.  
  21. 'type' is not defined...
  22.  
  23. >   fscanf(fp,"%s",comment);
  24.  
  25. 'comment' points to a space of 1 byte (the string "" above), which most
  26. likely is not even writeable because it is into the code section of your
  27. program... although you don't notice this easily because the Amiga does
  28. not use the MMU to make code sections read-only.
  29.  
  30. >   if (comment[0]=='#'){
  31. >     do{
  32. >        fscanf(fp, "%s",comment); 
  33.  
  34. Same here.
  35.  
  36. >          sprintf(buf,"%d",atoi(comment));  <---Crashes Here
  37.  
  38. No wonder.  First, 'buf' is only 1 byte long (type char), and even if the
  39. sprintf() call actually writes only an single integer digit, it adds a '\0'
  40. - thats already two characters.  Second, sprintf() requires an _array_ or
  41. a pointer to an _allocated_ memory buffer as the first argument, not only
  42. a single character. The compiler should have given you at least a warning
  43. here.
  44.  
  45. >          }
  46. >     while (strcmp(comment,buf)); 
  47.  
  48. >I compile it using:
  49. >gcc -o corr corr.c -m68040 -m68881
  50.  
  51. Try the options "-Wall -pedantic -ansi".
  52.  
  53. >If anyone can help me, I would greatly appreciate it.
  54.  
  55. Get a good book on C (for example K&R 2), and learn about pointers and arrays.
  56.  
  57.  
  58. Regards,
  59. Ingo
  60. -- 
  61. Ingo Wilken, CS Student, Univ.of Oldenburg, FRG | I'll admit dat I'm not one
  62. http://www.informatik.uni-oldenburg.de/~ingo/   | of da beautiful people, but
  63. E-mail: Ingo.Wilken@informatik.uni-oldenburg.de | I'm not as ugly as da spooks
  64. IRC: Nobody or TeaMan                           | dat hang 'round here.  -Snarf
  65.